﻿Ext.define('app.system.system_role_auth.DocumentAuth', {
    extend: 'Ext.panel.Panel',
    layout: 'border',
    roleColumn: [],
    initComponent: function () {
        var me = this;
        me.items = [me.getRoleGrid(), me.getPermissionGrid()];
        me.callParent();
    },
    getRoleGrid: function () {
        var me = this;
        if (!this.roleGrid) {
            this.roleGrid = new Ext.grid.Panel({
                title: '角色列表',
                region: 'west',
                split: true,
                width: 300,
                border: false,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/role/GetRoles', rootProperty: 'result.items' }),
                columns: [
                    { header: "角色", dataIndex: 'name' },
                    { header: "显示名", dataIndex: 'displayName' },
                    {
                        header: "创建时间", dataIndex: 'creationTime', renderer: function (value, p, record) {
                            return Ext.util.Format.date(value, 'Y-m-d');
                        }
                    }
                ],
                listeners: {
                    select: function (grid, record, index, eOpts) {
                        me.getPermissionGrid().store.load({ params: { roleId: record.data.id } });
                    }
                }
            });
        }
        return this.roleGrid;
    },
    getPermissionGrid: function () {
        var me = this;
        if (!this.permissionGrid) {
            this.permissionGrid = new Ext.grid.Panel({
                title: '文档类型权限',
                tbar: [
                    { text: '保存', icon: '/Content/images/page_save.png', handler: function () { me.save(); } },
                    "-",
                    {
                        text: '全选', icon: '/Content/images/accept.png', handler: function () {
                            var store = me.permissionGrid.store;
                            for (var i = 0; i < store.getCount() ; i++) {
                                var record = store.getAt(i);
                                for (var j = 0; j < me.roleColumn.length; j++) {
                                    var c = me.roleColumn[j];
                                    record.data[c.name] = true;
                                }
                                //record.data.hasAuth = true;
                                record.commit();
                            }
                        }
                    },
                    "-",
                    {
                        text: '全不选', icon: '/Content/images/cancel.png', handler: function () {
                            var store = me.permissionGrid.store;
                            for (var i = 0; i < store.getCount() ; i++) {
                                var record = store.getAt(i);
                                for (var j = 0; j < me.roleColumn.length; j++) {
                                    var c = me.roleColumn[j];
                                    record.data[c.name] = false;
                                }
                               // record.data.hasAuth = true;
                                record.commit();
                            }
                        }
                    }
                ],
                region: 'center',
                border: false,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                store: [],
                columns: [],
                forceFit: true
            });

            me.store = Ext.create("app.ux.PrmStore", { url: '/api/services/app/role/GetDocumentPermission', autoLoad: false });
            me.store.load({ params: { roleId: 0 } });

            App.AjaxGet('/api/services/app/documentType/GetAll', function (data) {
                me.roleColumn = data.result;
                var _columns = [{ header: '文档权限', dataIndex: 'documentAuth' }];
                for (var i = 0; i < data.result.length; i++) {
                    var record = data.result[i];
                    _columns.push({ xtype: 'checkcolumn', sortable: false, header: record.name, dataIndex: Ext.util.Format.lowercase(record.name) });
                }
                me.permissionGrid.reconfigure(me.store, _columns);
            });
        }
        return this.permissionGrid;
    },
    save: function () {
        var me = this;
        var sel = me.getRoleGrid().getSelection();
        if (sel.length == 0) {
            Ext.Msg.alert('系统提示', '请选择授权角色');
            return false;
        }
        var roleId = sel[0].data.id;

        var arrys = [];
        var store = me.getPermissionGrid().store;
        for (var i = 0; i < store.getCount() ; i++) {
            var _record = store.getAt(i);
            arrys.push(_record.data);
        }
        App.AjaxPost('/api/services/app/role/SetDocumentPermission', { RoleId: roleId, records: arrys }, function () {
            store.load({ params: { roleId: roleId } });
        });

    }
});